package rbacControllers

import (
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego"
	"lazyops_go/models"
	"lazyops_go/tools"
	"strconv"
)

type UserController struct {
	beego.Controller
}

//用户管理首页，获取数据
func (c *UserController) IndexUser() {
	cur_user := c.GetSession("cur_user")
	is_super := c.GetSession("is_super")
	cur_user_menus := c.GetSession("cur_user_menus")
	user_list := models.QueryUserAll(cur_user.(string),is_super.(bool))
	role_list :=models.QueryRoleAll()
	c.Data["cur_user_menus"] = cur_user_menus
	c.Data["cur_user"] = cur_user
	c.Data["user_list"] = user_list
	c.Data["role_list"] = role_list
	c.Data["label"] = "用户管理"
	c.XSRFExpire = 7200
	c.Data["xsrf_token"] = c.XSRFToken()
	c.Layout = "base/base.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "rbac/userjs.html"
	c.TplName = "rbac/user.html"
}

// 创建用户
func (c *UserController) CreateUser() {
	username := c.GetString("username")
	nickname := c.GetString("nickname")
	password := c.GetString("password")

	is_super,_:= c.GetBool("is_super",false)
	status,_ := c.GetInt("status", 0)
	email := c.GetString("email")
	mobile := c.GetString("mobile")
	roles := c.GetString("role_ids")
	passwd_key := c.GetString("passwd_key")

	//eas密码解密
	eaes_password:= tools.DePwdCode(password,passwd_key)

	//定义role_list 切片（列表）
	var role_id_list []string

	err := json.Unmarshal([]byte(roles), &role_id_list)
	if err != nil {
		fmt.Println("json 转换失败")
	}

	//MD5加密
	en_passwd := tools.Md5Encode(eaes_password)

	//实例 User 结构体
	user_obj := models.User{}
	user_obj.UserName = username
	user_obj.NickName = nickname
	user_obj.UserPwd = en_passwd
	user_obj.IsSuper = is_super
	user_obj.Status = status
	user_obj.Email = email
	user_obj.Mobile = mobile

	//实例Role结构体
	var role_list [] models.Role

	for i,_ := range role_id_list {
		//把string 转成int
		id, _ := strconv.Atoi(role_id_list[i])
		//获取role对象
		role := models.QueryRoleOne(id)
		role_list = append(role_list,role)
	}
	user_obj.Roles = role_list
	// 添加用户
	models.AddUser(user_obj)

	c.Data["json"] = "add Success"

	//将数据以json格式返回给前端
	c.ServeJSON()
}

//通过ID获取用户信息
func (c *UserController) GetUserById(){
	user_id,_ := c.GetInt("user_id",0)
	user_obj := models.QueryUserOne(user_id)
	c.Data["json"] =  user_obj
	c.ServeJSON()
}


//修改用户信息
func (c *UserController) EditUser()  {
	user_id,_:= c.GetInt("user_id", 0)
	username := c.GetString("username")
	nickname := c.GetString("nickname")
	password := c.GetString("password")
	is_super,_:= c.GetBool("is_super",false)
	status,_ := c.GetInt("status", 0)
	email := c.GetString("email")
	mobile := c.GetString("mobile")
	roles := c.GetString("role_ids")
	passwd_key := c.GetString("passwd_key")

	//定义role_list 切片（列表）
	var role_id_list []string

	err := json.Unmarshal([]byte(roles), &role_id_list)
	if err != nil {
		fmt.Println("json 转换失败")
	}
	//实例 User 结构体
	user_obj := models.User{}

	if password != ""{
		eaes_password:= tools.DePwdCode(password,passwd_key)
		en_passwd := tools.Md5Encode(eaes_password)
		user_obj.UserPwd = en_passwd
	}
	user_obj.UserName = username
	user_obj.NickName = nickname
	user_obj.IsSuper = is_super
	user_obj.Status = status
	user_obj.Email = email
	user_obj.Mobile = mobile

	//实例Role结构体
	var role_list [] models.Role

	for i,_ := range role_id_list {
		//把string 转成int
		id, _ := strconv.Atoi(role_id_list[i])
		//获取role对象
		role := models.QueryRoleOne(id)
		role_list = append(role_list,role)
	}

	user_obj.Roles = role_list
	// 添加用户
	models.UpdateUser(user_id,user_obj)

	c.Data["json"] = "修改成功"
	//将数据以json格式返回给前端
	c.ServeJSON()
}


//修改用户信息
func (c *UserController) ChangeUserPwd()  {
	user_id,_:= c.GetInt("user_id", 0)
	password := c.GetString("password")
	passwd_key := c.GetString("passwd_key")


	old_obj := models.QueryUserOne(user_id)
	//实例 User 结构体
	user_obj := models.User{}

	if password != ""{
		eaes_password:= tools.DePwdCode(password,passwd_key)
		en_passwd := tools.Md5Encode(eaes_password)
		user_obj.UserPwd = en_passwd
	}

	//继承原来的用户状态和超管,否则会被设置成默认值0
	user_obj.IsSuper = old_obj.IsSuper
	user_obj.Status = old_obj.Status
	//修改密码
	models.UpdateUserPwd(user_id,user_obj)

	c.Data["json"] = "修改成功"
	//将数据以json格式返回给前端
	c.ServeJSON()
}


//删除用户
func (c *UserController) DelUser() {
	user_id,_:= c.GetInt("user_id", 0)
	models.DeleteUser(user_id)
	c.Data["json"] = "删除成功"
	c.ServeJSON()
}


//搜索用户
func (c *UserController) SearchUser() {
	key := c.Ctx.Input.Param(":key")
	cur_user := c.GetSession("cur_user")
	is_super := c.GetSession("is_super")
	user_list := models.QueryUserSearch(key,cur_user.(string),is_super.(bool))
	cur_user_menus := c.GetSession("cur_user_menus")

	c.Data["cur_user"] = cur_user
	c.Data["cur_user_menus"] = cur_user_menus
	c.Data["key"] = key
	c.Data["user_list"] = user_list
	c.Data["label"] = "用户管理"
	c.XSRFExpire = 7200
	c.Data["xsrf_token"] = c.XSRFToken()
	c.Layout = "base/base.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "rbac/userjs.html"
	c.TplName = "rbac/user.html"
}

